package com.zxx.mvpPractice.ui.fragment;

import android.app.ProgressDialog;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;

import com.google.common.base.Preconditions;
import com.zxx.mvpPractice.R;
import com.zxx.mvpPractice.base.BaseFragment;
import com.zxx.mvpPractice.presenter.LoginFragmentPresenter;
import com.zxx.mvpPractice.presenter.contract.LoginFragmentContract;
import com.zxx.mvpPractice.ui.activitys.LoginActivity;
import com.zxx.mvpPractice.utils.EventUtil;
import com.zxx.mvpPractice.utils.JumpUtil;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 用户名密码登录
 * Created by Administrator on 2016/12/30.
 */
public class LoginPwdFragment extends BaseFragment<LoginFragmentContract.Presenter> implements LoginFragmentContract.View {

    @BindView(R.id.edt_login_username)
    EditText edt_login_username;
    @BindView(R.id.edt_login_password)
    EditText edt_login_password;
    private ProgressDialog dialog;
    private LoginActivity mActivity;


    @Override
    protected int getLayout() {
        return R.layout.fragment_login_pwd;
    }


    @OnClick({R.id.bt_login_submit, R.id.tv_forget_pwd, R.id.tv_code_login, R.id.rl_title_left})
    void clickEvent(View view) {
        switch (view.getId()) {
            case R.id.rl_title_left:
                EventUtil.showToast(mContext, "iv_title_left");
                mActivity.jumpToRegisterLogin();
                break;
            case R.id.bt_login_submit:
                String username = edt_login_username.getText().toString();
                String password = edt_login_password.getText().toString();
                mPresenter.bombUserLogin(username, password, 1);
                break;
            case R.id.tv_forget_pwd:
                mActivity.jumpToMissPwd();
                break;
            case R.id.tv_code_login:
                mActivity.jumpToRegisterLogin();
                break;
        }
    }

    @Override
    protected void initView(LayoutInflater inflater) {
        mActivity = (LoginActivity) mContext;
        mPresenter = new LoginFragmentPresenter(this);
        dialog = new ProgressDialog(mContext);
        dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        dialog.setMessage("登陆中...");
    }

    @Override
    public boolean onBackPressedSupport() {
        mActivity.jumpToRegisterLogin();
        return true;
    }

    @Override
    public void onResume() {
        super.onResume();
    }

    @Override
    public void onPause() {
        super.onPause();
    }

    @Override
    public void showProgressDialog() {
        dialog.show();
    }

    @Override
    public void hideProgressDialog() {
        dialog.dismiss();
    }

    @Override
    public void LoginSuccess() {
        JumpUtil.go2MainActivity(mContext);
        getActivity().overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
    }

    @Override
    public void loginFail() {

    }


    @Override
    public void showMessage(String text) {
        EventUtil.showToast(mContext, text);
    }

    @Override
    public void setPresenter(LoginFragmentContract.Presenter presenter) {
        mPresenter = Preconditions.checkNotNull(presenter);
    }

    @Override
    public void showError(String msg) {
        EventUtil.showToast(mContext, msg);
    }
}
